package com.shop.admin.service.pink;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.admin.dao.pink.StorePinkDao;
import com.shop.admin.dao.pink.StoreSnapPinkDao;
import com.shop.admin.property.BusinessProps;
import com.shop.admin.dao.store.StoreOrderDao;
import com.shop.domain.pink.StorePink;
import com.shop.domain.pink.StoreSnapPink;
import com.shop.domain.store.StoreOrder;
import com.shop.util.DateUtil;
import com.shop.util.http.HttpUtil;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
@Transactional
public class StoreOrderService {

    @Autowired
    private StorePinkDao storePinkDao;
    @Autowired
    private StoreOrderDao storeOrderDao;
    @Autowired
    private BusinessProps businessProps;
    @Autowired
    private StoreSnapPinkDao storeSnapPinkDao;

    /**
     * 拼团订单支付状态同步
     *
     * @param orderNo
     * @return
     */
    private BaseResponse pinkCheckPayStatus(String orderNo) {
        String url = businessProps.getFrontApiHost() + "/userPink/checkPinkPayStatus";

        // request body
        JSONObject reqBody = new JSONObject();
        reqBody.put("orderNo", orderNo);

        // request
        Map<String, String> header = new HashMap<>();
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求同步拼团订单状态异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        return JSON.parseObject(respBodyStr, BaseResponse.class);
    }

    /**
     * 普通订单支付状态同步
     *
     * @param orderNo
     * @return
     */
    private BaseResponse orderCheckPayStatus(String orderNo) {
        String url = businessProps.getFrontApiHost() + "/storeOrder/checkPayStatus";

        // request body
        JSONObject reqBody = new JSONObject();
        reqBody.put("orderNo", orderNo);

        // request
        Map<String, String> header = new HashMap<>();
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求同步普通订单状态异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        return JSON.parseObject(respBodyStr, BaseResponse.class);
    }


    /**
     * 抢购拼团订单支付状态同步
     *
     * @param orderNo
     * @return
     */
    private BaseResponse snapPinkCheckPayStatus(String orderNo) {
        String url = businessProps.getFrontApiHost() + "/userSnapPink/checkPinkPayStatus";

        // request body
        JSONObject reqBody = new JSONObject();
        reqBody.put("orderNo", orderNo);

        // request
        Map<String, String> header = new HashMap<>();
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求同步拼团订单状态异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        return JSON.parseObject(respBodyStr, BaseResponse.class);
    }

    /**
     * 同步待支付订单
     * @return
     */
    public BaseResponse checkNeedPayOrder(int timeoutMin, int timeoutEnd, int limit) {
        long begin = System.currentTimeMillis();

        Date now = new Date();
        Date timeoutDate = DateUtil.dateAddMinute(now, timeoutMin);
        Date timeoutEndDate = DateUtil.dateAddMinute(now, timeoutEnd);

        // 获取拼团商品未支付订单
        long checkPinkBegin = System.currentTimeMillis();

        List<StorePink> pinkList = storePinkDao.findNeedPayList(timeoutDate, limit, timeoutEndDate);

        log.info("当前未支付的拼团订单待同步记录数 -> {}", pinkList.size());

        BaseResponse resp = null;
        for (StorePink pink : pinkList) {
            // 请求接口同步状态
            resp = this.pinkCheckPayStatus(pink.getOrderNo());
        }

        long checkPinkEnd = System.currentTimeMillis();

        log.info("当前未支付的拼团订单待同步记录耗时 -> {} ms", (checkPinkEnd - checkPinkBegin));

        // 获取普通商品未支付订单
        long checkOrderBegin = System.currentTimeMillis();

        List<StoreOrder> orderList = storeOrderDao.findNeedPayList(timeoutDate, limit, timeoutEndDate);

        log.info("当前未支付的普通订单待同步记录数 -> {}", orderList.size());

        resp = null;
        for (StoreOrder order : orderList) {
            // 请求接口同步状态
            resp = this.orderCheckPayStatus(order.getOrderNo());
        }

        long checkOrderEnd = System.currentTimeMillis();

        log.info("当前未支付的普通订单待同步记录耗时 -> {} ms", (checkOrderEnd - checkOrderBegin));



        // 获取抢购拼团未支付订单
        long checkSnapPinkBegin = System.currentTimeMillis();

        List<StoreSnapPink> snapPinkList = storeSnapPinkDao.findNeedPayList(timeoutDate, limit, timeoutEndDate);

        log.info("当前未支付的抢购订单待同步记录数 -> {}", orderList.size());

        resp = null;
        for (StoreSnapPink order : snapPinkList) {
            // 请求接口同步状态
            resp = this.snapPinkCheckPayStatus(order.getOrderNo());
        }

        long checkSnapPinkEnd = System.currentTimeMillis();

        log.info("当前未支付的普通订单待同步记录耗时 -> {} ms", (checkSnapPinkEnd - checkSnapPinkBegin));

        log.info("同步待支付订单耗时 -> {} ms", (checkSnapPinkEnd - begin));

        return new BaseResponse();
    }
}
